﻿using System;
using System.Collections.Generic;
using System.Linq;
using LB.Common;
using LB.Server.DAL;

namespace LB.Server.Core
{
    public class ServerManager : IServerManager
    {
        private readonly IBookRepository bookRepository;

        public ServerManager(IBookRepository bookRepository)
        {
            this.bookRepository = bookRepository;
        }

        public List<Book> GetSharedBoks()
        {
            var dbBooks = bookRepository.GetAllBooks();
            return (from dbBook in dbBooks select dbBook.ToCommonBook()).ToList();
        }

        public List<Book> GetBoks(int userId)
        {
            var dbBooks = bookRepository.GetAllBooks();
            return (from dbBook in dbBooks select dbBook.ToCommonBook()).ToList();
        }


        public Book GetBookById(int bookId)
        {
            var dbBook = bookRepository.GetBook(bookId);
            return dbBook.ToCommonBook();
        }

        public List<Book> GetbookByAuthorId(int authorId)
        {
            var dbBooks = bookRepository.GetBooksByAuthor(authorId);
            return (from dbBook in dbBooks select dbBook.ToCommonBook()).ToList();
        }

        public Book AddBook(Book newBook)
        {
            return bookRepository.AddBook(newBook.ToDalBook()).ToCommonBook();
        }

        public Chapter AddChapter(Chapter chapter)
        {
            return bookRepository.AddChapter(chapter.ToDalCahpter()).ToCommonChapter();
        }

        public List<Chapter> GetChapters(int bookId)
        {
            return (from c in bookRepository.GetChapters(bookId) select c.ToCommonChapter()).ToList();
        }
    }
}
